﻿using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using FreeSql.DataAnnotations;
using Model;


namespace DAL
{
    public class Order
    {
        //更新订单的账户，应用场景：用户修改账户
        public int UpdateOrderAct(string newact, string act)
        {
            try
            {
                if (newact != act)
                {
                    IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                    fsql.Update<OrderInfo>()
                        .Set(a => a.account, newact)
                        .Where(a => a.account == act)
                        .ExecuteAffrows();
                }
            }
            catch
            {
                return -1;
            }
            return 0;

        }
        public int InsertOrder(OrderInfo order)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                fsql.Insert(order)
                    .ExecuteAffrows();
                var col = mongodb.GetCollection<BsonDocument>("OrderList");
                var doc = new BsonDocument();
                doc["Oid"] = order.Oid;
                doc.Add("PidList", new BsonArray(order.PidList));//向Document添加数组的方式               
                doc.Add("OnumList", new BsonArray(order.Onum));
                col.InsertOne(doc);

            }
            catch
            {
                return -1;
            }
            return 0;
        }
        //根据用户account删除其所有订单
        public int DeleteOrder(string account)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                List<OrderInfo> orders = fsql.Select<OrderInfo>()
                    .Where(a => a.account == account)
                    .ToList();
                var col = mongodb.GetCollection<BsonDocument>("OrderList");
                for (int i = 0; i < orders.Count; i++)
                {
                    //定义获取“ID”值为account的查询条件
                    var query = new QueryDocument { { "Oid", orders[i].Oid } };
                    //执行删除操作
                    col.DeleteMany(query);
                }
                fsql.Delete<OrderInfo>()
                    .Where(a => a.account == account)
                    .ExecuteAffrows();

            }
            catch
            {
                return -1;
            }
            return 0;
        }
        public List<OrderInfo> GetOrderInfo(string account)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                List<OrderInfo> orders;
                orders = fsql.Select<OrderInfo>()
                    .Where(a => a.account == account)
                    .ToList();
                //account账户下的所有订单
                var collection = mongodb.GetCollection<BsonDocument>("OrderList");//获取OrderList集合

                for (int i = 0; i < orders.Count; i++)//为每一条订单在MongoDB数据库里查找对应的num和pid
                {
                    var query = new QueryDocument { { "Oid", orders[i].Oid } };//查询orders中对应的oid的数据
                    var doc = collection.Find<BsonDocument>(query).FirstOrDefault();
                    orders[i].PidList = new List<string>();
                    orders[i].Onum = new List<int>();
                    for (int j = 0; j < doc.GetValue("PidList").AsBsonArray.Count; j++)
                    {
                        string s = doc.GetValue("PidList").AsBsonArray[j].ToString();
                        int a = doc.GetValue("OnumList").AsBsonArray[j].ToInt32();
                        orders[i].PidList.Add(s);
                        orders[i].Onum.Add(a);
                    }
                }
                return orders;
            }
            catch
            {
                return null;
            }
        }
    }
}

